<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="1000" destroy-on-close @closed="$emit('closed')">
		<el-form :model="entityObj" :rules="rules" :disabled="mode == 'Show'" ref="dialogForm" label-width="100px">
			<el-row>
				<el-col :span="12">
					<el-form-item label-width="120px" label="分类编号" prop="materialTypeCode">
						<el-input v-model="entityObj.materialTypeCode" disabled/>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label-width="120px" label="分类名称" prop="materialTypeName">
						<el-input v-model="entityObj.materialTypeName" />
					</el-form-item>
				</el-col>
			</el-row>
			<el-row>
				<el-col :span="12" v-show="mode!='Add'">
					<el-form-item label-width="120px" label="归属分类" prop="parentMaterialTypeName">
						<el-input v-model="entityObj.parentMaterialTypeName" :disabled="true" />
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label-width="120px" label="状态"><el-switch v-model="entityObj.status" :active-value="1"
							:inactive-value="0" />
					</el-form-item>
				</el-col>
			</el-row>
			<el-row>
                <el-col :span="24">
                    <el-form-item label-width="120px" label="分类图片" prop="materialImage">
                        <sc-upload
                            v-model="entityObj.imageUrl"
                            :objName="'Supply/MaterialType'"
                            draggable
                            :uploadType="'OSS'"
                            :bucketName="'jtmc'"
                            :limit="1"
                            tip="最多上传1个文件,请上传图像格式文件"
                        />
                    </el-form-item>
                </el-col>
            </el-row>
			<el-row>
                <el-col :span="24">
                    <el-form-item label-width="120px" label="备注" >
                        <el-input v-model="entityObj.remarks" type="textarea" :rows="2"/>
                    </el-form-item>
                </el-col>
            </el-row>		
		</el-form>
		<template #footer v-if="mode != 'Show'">
			<el-button @click="visible = false">取 消</el-button>
			<el-button v-if="mode != 'show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>
export default {
	emits: ["success", "closed"],
	data() {
		return {
			mode: "Add",
			titleMap: {
				Add: "新增",
				Edit: "编辑",
				Show: "查看",
				AddSub: "增加子节点",
			},
			addNum: 1,
			visible: false,
			isSaveing: false,
			needSaveFlag: false,
			entityObj: {
				status:1,
				parentId:0
			},
			//验证规则
			rules: {
                materialTypeName: [{ required: true, type: "string", message: "请填写物料编码" }],
			},
			optionsTypeCode: [],
			optionsParentCode: [],
		};
	},
	methods: {
		//显示=
		open(mode = "Add") {
			this.mode = mode;
			this.visible = true;
			return this;
		},
		//表单提交方法
        submit() {
            this.$refs.dialogForm.validate(async (valid) => {
                if (valid) {
                    this.isSaveing = true;
                    try {
                        var returnValue = await this.$API.auth.PostProcessApi.post(this.mode != "Edit"?"material-type/create":"material-type/update",{...this.entityObj});
                        if (returnValue.status == 200) {
                            this.$message.success("操作成功");
                            this.$emit("fatherMethod");
                            this.isSaveing = false;
                            this.visible = false;
                        } else {
                            this.$message.error(
                                "操作失败:" + returnValue.msg
                            );
                        }
                    } catch (error) {
                        this.loading = false;
                        return false;
                    }
                }
            });
        },
		//表单注入数据
        async setData(data) {
			console.log(data)
			this.entityObj={...data};
        },
	},
	async mounted() {

	},
};
</script>

<style></style>
